package com.meizu.controller;

import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meizu.entity.RedisModel;
import com.meizu.service.IHibernateJpaService;
import com.meizu.service.jms.Producer;
import com.meizu.service.redis.RedisServiceImpl;

@Controller
public class SampleController {
	
	@Autowired
	private IHibernateJpaService hibernateJpaService;

    @RequestMapping("/meizu")
    @ResponseBody
    String home() {
        return "Hello World!XXX";
    }
    
    @RequestMapping("/save")
    @ResponseBody
    String save() {
    	hibernateJpaService.testHibernateJpaTransaction();
//    	hibernateJpaService.testQuery();
        return "test success!";
    }
    @RequestMapping("/list")
    @ResponseBody
    String uList() {
//    	hibernateJpaService.testHibernateJpaTransaction();
    	hibernateJpaService.testQuery();
        return "test success!";
    }
    
    @RequestMapping("/tsession/{name}")
    @ResponseBody
    public String TestSession(HttpServletRequest req, HttpServletResponse resp,@PathVariable String name){
        req.getSession().setAttribute("name", name);
        String a = (String) req.getSession().getAttribute("name");
        return a;
    }
    
    @Autowired
    private RedisServiceImpl service;

    //添加
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public void test() {
        System.out.println("start.....");
        RedisModel m = new RedisModel();
        m.setName("张三");
        m.setTel("1111");
        m.setAddress("深圳1");
        m.setRedisKey("zhangsanKey01");
        service.put(m.getRedisKey(), m, -1);

        RedisModel m2 = new RedisModel();
        m2.setName("张三2");
        m2.setTel("2222");
        m2.setAddress("深圳2");
        m2.setRedisKey("zhangsanKey02");
        service.put(m2.getRedisKey(), m2, -1);

        RedisModel m3 = new RedisModel();
        m3.setName("张三3");
        m3.setTel("2222");
        m3.setAddress("深圳2");
        m3.setRedisKey("zhangsanKey03");
        service.put(m3.getRedisKey(), m3, -1);

        System.out.println("add success end...");
    }

    //查询所有对象
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public Object getAll() {
        return service.getAll();
    }

    //查询所有key
    @RequestMapping(value = "/getKeys", method = RequestMethod.GET)
    @ResponseBody
    public Object getKeys() {
        return service.getKeys();
    }

    //根据key查询
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public Object get() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey02");
        return service.get(m.getRedisKey());
    }

    //删除
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    public void remove() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        service.remove(m.getRedisKey());
    }

    //判断key是否存在
    @RequestMapping(value = "/isKeyExists", method = RequestMethod.GET)
    @ResponseBody
    public void isKeyExists() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        boolean flag = service.isKeyExists(m.getRedisKey());
        System.out.println("zhangsanKey01 是否存在: "+flag);
    }

    //查询当前缓存的数量
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public Object count() {
        return service.count();
    }

    //清空所有key
    @RequestMapping(value = "/empty", method = RequestMethod.GET)
    @ResponseBody
    public void empty() {
        service.empty();
    }
    
    @Autowired  
	private Producer producer;
    
    @RequestMapping(value = "/testJms", method = RequestMethod.GET)
    @ResponseBody
    public void testJms() {
    	Destination destination = new ActiveMQQueue("TestQueue");
    	for(int i=0; i<100; i++){  
			producer.sendMessage(destination, "myname is chhliu!!!");  
		}
    	Destination destinationTopic = new ActiveMQTopic("TestTopic");
    	for(int i=0; i<100; i++){  
			producer.sendMessage(destination, "myname is weizifu!!!");  
		}
    }

}
